<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-11 15:38
 *
 * Version: 2.0
 */

namespace App\Modules\System\Services;


use App\Modules\System\Repositories\AuthRuleRepository;
use App\Modules\System\Repositories\RoleAuthRuleRepository;
use App\Modules\System\Repositories\UserStoreRepository;
use Flower\Core\Repository\Eloquent\BaseService;

class OperateService extends BaseService
{
	private $authRuleRepository;
	private $roleAuthRuleRepository;
	private $userStoreRepository;
	public function __construct(AuthRuleRepository $authRuleRepository,RoleAuthRuleRepository $roleAuthRuleRepository,UserStoreRepository $userStoreRepository)
	{
		$this->authRuleRepository = $authRuleRepository;
		$this->roleAuthRuleRepository = $roleAuthRuleRepository;
		$this->userStoreRepository = $userStoreRepository;
	}


	/**
	 * 权限检测
	 * @param $userId
	 * @param $storeId
	 * @param $rule
	 * @return bool
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function checkAuth($userId,$storeId,$rule){
		// 获取用户在某店的角色列表
		$roleList = $this->userStoreRepository->getRoleList($userId,$storeId);
		// 获取当前访问的路由在表中的信息
		$ruleInfo = $this->authRuleRepository->getByRule(substr($rule,'5'));
		if(empty($ruleInfo) || empty($roleList)) return false;
		// 获取当前角色的所有权限List
		$ruleList = $this->roleAuthRuleRepository->getRoleAuth($roleList);
		return in_array($ruleInfo->id,$ruleList);
	}
}